/* 
 * Kyle Woodworth 5/4/2012
 * 
 * Sets up data dynamically for data/graphing
 */


$(document).ready(loadPlanEvents())

function loadPlanEvents(){
    var planSelect = document.forms["planForm"].elements["planSelect"];
    var planId = planSelect.options[planSelect.selectedIndex].value;
    
    $.ajax({
        url: "Analytics?action=getEvents&planId="+planId,
        dataType: "json",
        success: function(eventsObject) {
            var events = eval(eventsObject)
            var innerHtml = "";
            $.each(events.event, function(key,value){
                innerHtml += "<option value="+value.id+">"+value.name+"</option>";
            })
            
            document.forms["eventForm"].elements["eventSelect"].innerHTML = innerHtml;
            createChart();
        },
        error: function(data,data2,data3) {
            alert("Failed: "+data+" \n\n"+data2+"\n\n"+data3);
        }
    });
}

function createChart(){
    var chart;
    var eventSelect = document.forms["eventForm"].elements["eventSelect"];
    var eventId = eventSelect.options[eventSelect.selectedIndex].value;
    $.ajax({
        url: "Analytics?action=generateChart&eventId="+eventId+"&url="+document.URL,
        dataType: "json",
        success: function(chartObject) {
            chart = new Highcharts.Chart(chartObject);
        },
        error: function(data,data2,data3) {
            alert("Failed: "+data+" \n\n"+data2+"\n\n"+data3);
        }
    });
    
}

